home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DDJ0992.ARJ / XFPROJ.C < prev    next >
Text File  |  1992-04-03  |  3KB  |  69 lines

  1. /* Transforms all vertices in the specified polygon-based object into
  2.    view space, then perspective projects them to screen space and maps
  3.    them to screen coordinates, storing the results in the object.
  4.    Recalculates object->view transformation because only if the
  5.    transform changes would we bother to retransform the vertices.
  6.    Also recalculates CenterInView and moves the object through the
  7.    object list accordingly.
  8.  */
  9. #include <math.h>
  10. #include "polygon.h"
  11.  
  12. static Point3 CenterPoint = {0, 0, 0};
  13.  
  14. void XformAndProjectPObject(PObject * ObjectToXform)
  15. {
  16.    int i, NumPoints = ObjectToXform->NumVerts;
  17.    int NumRealPoints = ObjectToXform->NumRealVerts;
  18.    Point3 * Points = ObjectToXform->VertexList;
  19.    Point3 * XformedPoints = ObjectToXform->XformedVertexList;
  20.    Point3 * ProjectedPoints =
  21.          ObjectToXform->ProjectedVertexList;
  22.    Point * ScreenPoints = ObjectToXform->ScreenVertexList;
  23.  
  24.    /* Recalculate the object->view transform */
  25.    ConcatXforms(WorldViewXform, ObjectToXform->XformToWorld,
  26.          ObjectToXform->XformToView);
  27.  
  28.    /* Recalculate CenterInView */
  29.    XformVec(ObjectToXform->XformToView, (Fixedpoint *) &CenterPoint,
  30.          (Fixedpoint *) &ObjectToXform->CenterInView);
  31.  
  32.    /* Apply that new transformation and project all polygon vertices (not
  33.       unit normal endpoints yet, though) */
  34.    for (i=0; i<NumRealPoints; i++, Points++, XformedPoints++,
  35.          ProjectedPoints++, ScreenPoints++) {
  36.       /* Transform to view space */
  37.       XformVec(ObjectToXform->XformToView, (Fixedpoint *) Points,
  38.             (Fixedpoint *) XformedPoints);
  39.  
  40.       /* Perspective-project to screen space */
  41.       ProjectedPoints->X =
  42.             FixedMul(FixedDiv(XformedPoints->X, XformedPoints->Z),
  43.             DOUBLE_TO_FIXED(PROJECTION_RATIO * (SCREEN_WIDTH/2)));
  44.       ProjectedPoints->Y =
  45.             FixedMul(FixedDiv(XformedPoints->Y, XformedPoints->Z),
  46.             DOUBLE_TO_FIXED(PROJECTION_RATIO * (SCREEN_WIDTH/2)));
  47.       ProjectedPoints->Z = XformedPoints->Z;
  48.  
  49.       /* Convert to screen coordinates. The Y coord is negated to
  50.          flip from increasing Y being up to increasing Y being down,
  51.          as expected by the polygon filler. Add in half the screen
  52.          width and height to center on the screen */
  53.       ScreenPoints->X = ((int) ((ProjectedPoints->X +
  54.             DOUBLE_TO_FIXED(0.5)) >> 16)) + SCREEN_WIDTH/2;
  55.       ScreenPoints->Y = (-((int) ((ProjectedPoints->Y +
  56.             DOUBLE_TO_FIXED(0.5)) >> 16))) + SCREEN_HEIGHT/2;
  57.    }
  58.  
  59.    /* Now transform the unit normal endpoints into view space, but there's
  60.       no reason to waste time getting them into screen space or screen
  61.       coordinates */
  62.    for (i=NumRealPoints; i<NumPoints; i++, Points++, XformedPoints++) {
  63.       /* Transform to view space */
  64.       XformVec(ObjectToXform->XformToView, (Fixedpoint *) Points,
  65.             (Fixedpoint *) XformedPoints);
  66.    }
  67. }
  68.  
  69.